import { createWebHashHistory, createRouter } from 'vue-router';
import VerticalLayout from '@/layout/VerticalLayout.vue';
import BasicsLayout from '@/layout/BasicsLayout.vue';

// 拿到全部业务路由
const ctx = import.meta.globEager('./components/*.js');
const routes = {};
for (const key in ctx) {
	const value = Object.values(ctx[key]);
	const segments = key.split('/');
	const fileName = segments[segments.length - 1];
	const componentName = fileName.split('.')[0];
	routes[componentName] = [];
	value.forEach((x) => {
		if (x instanceof Array) {
			x.forEach((r) => routes[componentName].push(r));
		} else if (x instanceof Object) {
			routes[componentName].push(x);
		}
	});
}
const router = createRouter({
	history: createWebHashHistory(),
	routes: [
		{
			path: '/',
			redirect: routes.BasicsLayout[0].path,
			component: BasicsLayout,
			meta: {
				title: '首页',
			},
			children: routes.BasicsLayout,
		},
		{
			path: '/Login',
			name: 'Login',
			component: () => import('@/views/Login/index.vue'),
		},
		{
			path: '/vertical',
			redirect: routes.VerticalLayout[0].path,
			component: VerticalLayout,
			children: routes.VerticalLayout,
		},
	],
});

export default router;
